#pragma once

#include <iostream>
#include <pthread.h>

class Mutex
{
public:
    Mutex(pthread_mutex_t *lock_p = nullptr)
        :_lock_p(lock_p)
    {}

    void lock()
    {
        if(_lock_p) pthread_mutex_lock(_lock_p);//指针不为空，我们才去解锁互斥锁
    }

    void unlock()
    {
        if(_lock_p) pthread_mutex_unlock(_lock_p);
    }

    ~Mutex() {}
private:
    pthread_mutex_t *_lock_p;//未来外部传入的互斥锁
};

class LockGuard
{
public:
    LockGuard(pthread_mutex_t *mutex)
        :_mutex(mutex)//拿指针构造Mutex类对象
    {
        _mutex.lock();//在构造函数中进行加锁
    }

    ~LockGuard()
    {
        _mutex.unlock();//在析构函数中进行解锁
    }

private:
    Mutex _mutex;
};
